package fmod.org.smali.myapplication.views;


import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;
import android.widget.ViewFlipper;

public class PlayerDiscViewFlipperTemp extends ViewFlipper {
    private static String TAG = "PlayerDiscViewFlipper";
    private GestureDetector mGestureDetector;
    private Scroller mScroller;
    private OnClickListener c;
    private PlayerDiscViewFlipperListener mViewFlipperListener;
    private float mSlideDistanceX = 0.0F;
    private int mCurrentViewLeftActionDown = 0;
    private int mNextViewIndex = -1;
    private boolean isStartFlip = false;
    private boolean isSlideRight;
    private boolean isFlipActionFromButton;
    private boolean isSuccessFlipToRight;
    private boolean isSuccessFlipToLeft;
    private boolean q;
    private boolean isScrollFinish;
    private long mActionDownTime = 0L;
    private int u = 0;
    private OnGestureListener mOnGestureListener = new SimpleOnGestureListener() {
        public boolean onDown(MotionEvent var1) {
            PlayerDiscViewFlipperTemp.this.mCurrentViewLeftActionDown = PlayerDiscViewFlipperTemp.this.getCurrentView().getLeft();
            PlayerDiscViewFlipperTemp.this.mSlideDistanceX = 0.0F;
            PlayerDiscViewFlipperTemp.this.isFlipActionFromButton = false;
            return true;
        }

        public boolean onScroll(MotionEvent downEvent, MotionEvent currentEvent, float var3, float var4) {
            Log.i(TAG, "var1x=" + downEvent.getX() + "--var2x=" + currentEvent.getX());
            if (downEvent != null && currentEvent != null) {
                mSlideDistanceX = downEvent.getX() - currentEvent.getX();
                PlayerDiscViewFlipperTemp var6 = PlayerDiscViewFlipperTemp.this;
                boolean isSlideRight;
                if (mSlideDistanceX > 0.0F) {
                    isSlideRight = true;
                } else {
                    isSlideRight = false;
                }

                var6.isSlideRight = isSlideRight;
                if (!PlayerDiscViewFlipperTemp.this.isStartFlip) {
                    PlayerDiscViewFlipperTemp.this.isStartFlip = true;
                    PlayerDiscViewFlipperTemp.this.isFlipActionFromButton = false;
                    if (PlayerDiscViewFlipperTemp.this.mViewFlipperListener != null) {
                        PlayerDiscViewFlipperListener var7 = PlayerDiscViewFlipperTemp.this.mViewFlipperListener;
                        if (PlayerDiscViewFlipperTemp.this.mSlideDistanceX > 0.0F) {
                            isSlideRight = true;
                        } else {
                            isSlideRight = false;
                        }

                        var7.flip(isSlideRight);
                    }
                }
                //向左翻页成功成功的瞬间a(FALSE)
                if (PlayerDiscViewFlipperTemp.this.getCurrentView().getLeft() < -PlayerDiscViewFlipperTemp.this.getWidth() / 2 && !PlayerDiscViewFlipperTemp.this.isSuccessFlipToLeft) {
                    if (PlayerDiscViewFlipperTemp.this.mViewFlipperListener != null) {
                        PlayerDiscViewFlipperTemp.this.mViewFlipperListener.flip(Boolean.valueOf(false));
                    }

                    PlayerDiscViewFlipperTemp.this.q = false;
                    PlayerDiscViewFlipperTemp.this.isSuccessFlipToRight = false;
                    PlayerDiscViewFlipperTemp.this.isSuccessFlipToLeft = true;
                    //向右翻页成功瞬间a(TRUE)
                } else if (PlayerDiscViewFlipperTemp.this.getCurrentView().getLeft() > PlayerDiscViewFlipperTemp.this.getWidth() / 2 && !PlayerDiscViewFlipperTemp.this.isSuccessFlipToRight) {
                    if (PlayerDiscViewFlipperTemp.this.mViewFlipperListener != null) {
                        PlayerDiscViewFlipperTemp.this.mViewFlipperListener.flip(Boolean.valueOf(true));
                    }

                    PlayerDiscViewFlipperTemp.this.q = false;
                    PlayerDiscViewFlipperTemp.this.isSuccessFlipToRight = true;
                    PlayerDiscViewFlipperTemp.this.isSuccessFlipToLeft = false;
                } else if ((PlayerDiscViewFlipperTemp.this.getCurrentView().getLeft() >= -PlayerDiscViewFlipperTemp.this.getWidth() / 2 && PlayerDiscViewFlipperTemp.this.getCurrentView().getLeft() < 0 || PlayerDiscViewFlipperTemp.this.getCurrentView().getLeft() <= PlayerDiscViewFlipperTemp.this.getWidth() / 2 && PlayerDiscViewFlipperTemp.this.getCurrentView().getLeft() > 0) && !PlayerDiscViewFlipperTemp.this.q) {
                    if (PlayerDiscViewFlipperTemp.this.mViewFlipperListener != null) {
                        PlayerDiscViewFlipperTemp.this.mViewFlipperListener.flip((Boolean) null);
                    }
                    PlayerDiscViewFlipperTemp.this.q = true;
                    PlayerDiscViewFlipperTemp.this.isSuccessFlipToRight = false;
                    PlayerDiscViewFlipperTemp.this.isSuccessFlipToLeft = false;
                }

                PlayerDiscViewFlipperTemp.this.requestLayout();
                return true;
            } else {
                return false;
            }
        }

        public boolean onSingleTapUp(MotionEvent var1) {
            if (PlayerDiscViewFlipperTemp.this.c != null) {
                PlayerDiscViewFlipperTemp.this.c.onClick(PlayerDiscViewFlipperTemp.this);
            }

            return true;
        }
    };

    public PlayerDiscViewFlipperTemp(Context var1, AttributeSet var2) {
        super(var1, var2);
        mGestureDetector = new GestureDetector(this.getContext(), this.mOnGestureListener);
        mScroller = new Scroller(this.getContext(), new DecelerateInterpolator());
    }

    public void a(boolean var1) {
        byte var2 = 1;
        if (!mScroller.isFinished()) {
            mScroller.forceFinished(true);
            if (this.u > 0) {
                if (this.mViewFlipperListener != null) {
                    this.mViewFlipperListener.flip(false, true, var1);
                }

                --this.u;
            }
        }

        if (this.mViewFlipperListener != null) {
             this.mViewFlipperListener.flip(var1);
        }


        this.isSlideRight = var1;
        isFlipActionFromButton = true;
        mNextViewIndex = this.getDisplayedChild() + 1;
        ++this.u;
        Scroller var6 = mScroller;
        int var3 = this.getWidth();
        if (var1) {
            var2 = -1;
        }

        var6.startScroll(0, 0, var3 * var2, 0, 400);
        this.requestLayout();
    }

    public View getNextView() {
        byte var1 = 1;
        if (this.getChildAt(1) == this.getCurrentView()) {
            var1 = 0;
        }

        return this.getChildAt(var1);
    }

    protected void onLayout(boolean var1, int var2, int var3, int var4, int var5) {
        var1 = false;
        if (mScroller.computeScrollOffset()) {
            var2 = mScroller.getCurrX();
        } else {
            var2 = (int) ((float) mCurrentViewLeftActionDown - this.mSlideDistanceX);
        }

        var3 = var2;
        //如果从手指现在的位置在按下时的
        if (var2 <= -this.getMeasuredWidth()) {
            var3 = var2;
            if (this.isSlideRight) {
                var3 = -this.getMeasuredWidth();
            }
        }

        var2 = var3 + this.getMeasuredWidth();
        this.getCurrentView().layout(var3, 0, var2, this.getMeasuredHeight());
        var4 = this.getMeasuredWidth();
        if (!this.isSlideRight) {
            var3 -= this.getMeasuredWidth();
            var2 = var3;
            if (var3 > 0) {
                var2 = 0;
            }

            var4 = this.getMeasuredWidth();
            var3 = var2;
            var2 += var4;
        } else {
            var4 += var2;
            var3 = var2;
            var2 = var4;
        }

        if (var3 < this.getMeasuredWidth() || var3 > 0) {
            View var6 = this.getNextView();
            var6.setVisibility(View.VISIBLE);
            var6.layout(var3, 0, var2, this.getMeasuredHeight());
        }

        if (!mScroller.isFinished()) {
            this.post(new Runnable() {
                public void run() {
                    PlayerDiscViewFlipperTemp.this.requestLayout();
                }
            });
        } else {
            if (mNextViewIndex != -1) {
                this.setDisplayedChild(mNextViewIndex);
                mNextViewIndex = -1;
            } else {
                var1 = true;
            }

            if (this.u > 0) {
                if (this.mViewFlipperListener != null) {
                    PlayerDiscViewFlipperTemp.this.mViewFlipperListener.flip(var1, PlayerDiscViewFlipperTemp.this.isFlipActionFromButton, PlayerDiscViewFlipperTemp.this.isSlideRight);
                }
                --this.u;
                return;
            }
        }

    }


    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mActionDownTime = System.currentTimeMillis();
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
                if (mSlideDistanceX != 0.0F || isFlipActionFromButton) {
                    ++this.u;
                }
                this.q = false;
                this.isSuccessFlipToRight = false;
                this.isSuccessFlipToLeft= false;
                this.isStartFlip = false;
                float speedX = mSlideDistanceX / (float) (System.currentTimeMillis() - this.mActionDownTime);
                mSlideDistanceX = 0.0F;
                int margenLeft = getCurrentView().getLeft();
                if (margenLeft <= getMeasuredWidth() / 2 && (double) speedX >= -1.7D) {
                    if (margenLeft > 0) {
                        if (isScrollFinish) {
                            mScroller.startScroll(margenLeft, 0, -this.getCurrentView().getLeft(), 0, 400);
                        }
                    } else if (margenLeft >= -getMeasuredWidth() / 2 && (double) speedX <= 1.7D) {
                        if (margenLeft < 0 && isScrollFinish) {
                            mScroller.startScroll(margenLeft, 0, -margenLeft, 0, 400);
                        }
                    } else {
                        mNextViewIndex = getDisplayedChild() + 1;
                        if (isScrollFinish) {
                            mScroller.startScroll(margenLeft, 0, -this.getMeasuredWidth() - margenLeft, 0, 400);
                        }
                    }
                } else {
                    mNextViewIndex = this.getDisplayedChild() + 1;
                    if (isScrollFinish) {
                        mScroller.startScroll(margenLeft, 0, this.getMeasuredWidth() - this.getCurrentView().getLeft(), 0, 400);
                    }
                }

                this.requestLayout();
            }

            boolean isFinishScroll = mScroller.isFinished();
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                isScrollFinish = isFinishScroll;
            }

            if (isFinishScroll) {
                if (!isScrollFinish) {
                    isScrollFinish = true;
                    MotionEvent var5 = MotionEvent.obtain(event);
                    var5.setAction(MotionEvent.ACTION_DOWN);
                    mGestureDetector.onTouchEvent(var5);
                }

                mGestureDetector.onTouchEvent(event);
            }

            return true;

    }

    public void setOnClickListener(OnClickListener var1) {
        this.c = var1;
    }

    public void setOnPlayerDiscListener(PlayerDiscViewFlipperListener var1) {
        this.mViewFlipperListener = var1;
    }


    public interface PlayerDiscViewFlipperListener {
        //向左本次翻页成功瞬间  if null 本次翻页失败
        void flip(Boolean isSuccessToLeft);
        //开始向左
        void flip(boolean isToLeft);
        //翻页完成
        void flip(boolean isFailure, boolean isActionFromButton, boolean isToLeft);

    }
}
